package com.example.service.impl;

import com.example.entity.dto.salesSlipBodyDto;
import com.example.entity.dto.salesSlipDto;
import com.example.entity.hz.ShenHe;
import com.example.entity.vo.CustomerInfoVo;
import com.example.entity.yifei.Coptg;
import com.example.entity.yifei.Copth;
import com.example.mapper.CommonMapper;
import com.example.mapper.SalesMapper;
import com.example.service.SalesService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : ssy
 * @date: 2024-04-24
 * @Description:
 */
@Service
public class SalesServiceImpl implements SalesService {

    @Resource
    private SalesMapper salesMapper;

    @Resource
    private CommonMapper commonMapper;

    @Override
    public Map<String, String> salesSlipByAdvice(salesSlipDto salesSlipDto) throws UnknownHostException {
        int i = 1;
        //原币销货金额
        double xhje = 0.0;
        //原币销货税额
        double xhse = 0.0;
        //本币销货金额
        double bbxhje = 0.0;
        //本币销货税额
        double bbxhse = 0.0;
        //件数
        double js = 0.0;
        //数量
        double sl = 0.0;
        Map<String,String> map = new HashMap<>();
        Coptg coptg = new Coptg();
        coptg.setTG001("ASHO");
        CustomerInfoVo customerInfo = salesSlipDto.getCustomerInfoVo();
        coptg.setCREATOR(salesSlipDto.getCreator());
        String usrGroup = commonMapper.getUsrGroup(salesSlipDto.getCreator());
        String company = commonMapper.getCompany();
        coptg.setUSR_GROUP(usrGroup);
        coptg.setCOMPANY(company);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

        // 格式化时间
        String yyyyMM = currentTime.format(formatter);
        String yyyyMMdd = currentTime.format(formatter1);
        String create_date = currentTime.format(formatter2);

        coptg.setCREATE_DATE(create_date);
        BigDecimal flag = new BigDecimal("1");
        coptg.setFLAG(flag);
        //TODO 可以根据单据性质产生单号
        Long dh = commonMapper.getDH("COPTG","TG002");
        Long time = Long.parseLong(yyyyMMdd + "001");
        if (dh == null ||dh < time) {
            dh = time;
        } else {
            dh++;
        }
        coptg.setTG002(""+dh);
        coptg.setTG003(yyyyMMdd);
        coptg.setTG004(customerInfo.getCustomerNo());
        coptg.setTG005(salesSlipDto.getCustomerInfoVo().getDeptNo());
        coptg.setTG006(salesSlipDto.getCustomerInfoVo().getEmpNo());
        coptg.setTG008(customerInfo.getAddressNo1());
        coptg.setTG009(customerInfo.getAddressNo2());
        //TODO 工厂只有一个暂时写死
        coptg.setTG010("01");
        coptg.setTG011(customerInfo.getCurrency());
        if (customerInfo.getCurrency().equals("RMB")){
            coptg.setTG012(1.0000000);
        }else {
            coptg.setTG012(salesMapper.selectHL(customerInfo.getCurrency()));
        }
        ArrayList<Copth> copths = new ArrayList<>();
        for (salesSlipBodyDto salesSlipBodyDto : salesSlipDto.getSalesSlipBodyDtos()) {
            Copth copth = new Copth();
            String xh = String.format("%04d", i++);
            copth.setCOMPANY(company);
            copth.setCREATOR(salesSlipDto.getCreator());
            copth.setUSR_GROUP(usrGroup);
            copth.setCREATE_DATE(create_date);
            copth.setFLAG(flag);
            copth.setTH001("ASHO");
            copth.setTH002(""+dh);
            copth.setTH003(xh);
            String order = salesSlipBodyDto.getSalesAdvice().getDb()+salesSlipBodyDto.getSalesAdvice().getDh()+salesSlipBodyDto.getSalesAdvice().getXh();
            Map<String,Object> map1 = salesMapper.getBodyInfo(order);
            if (map1==null){
                map.put("此单号未查询到数据:",order);
                continue;
            }
            copth.setTH004((String) map1.get("TO007"));
            copth.setTH005((String) map1.get("TO008"));
            copth.setTH006((String) map1.get("TO009"));
            copth.setTH007((String) map1.get("TO011"));
            BigDecimal TO034 = (BigDecimal) map1.get("TO034");
            Double shipmentQuantity = salesSlipBodyDto.getSalesAdvice().getShipmentQuantity();
            copth.setTH008(shipmentQuantity);
            sl += shipmentQuantity;
            copth.setTH009((String) map1.get("TO010"));
            //TODO 库存数量不知道哪里取默认都为0
            copth.setTH010(0.000000);
            BigDecimal TO020 = (BigDecimal) map1.get("TO020");
            copth.setTH012(TO020.doubleValue());
            copth.setTH014((String) map1.get("TO004"));
            copth.setTH015((String) map1.get("TO005"));
            copth.setTH016((String) map1.get("TO006"));
            copth.setTH017((String) map1.get("TO014"));
            copth.setTH018(salesSlipBodyDto.getRemark());
            copth.setTH019((String) map1.get("TO026"));
            copth.setTH020("N");
            copth.setTH021("N");
            BigDecimal TO019 = (BigDecimal) map1.get("TO019");
            copth.setTH024(TO019.doubleValue());
            //TODO 折扣率默认1.0000
            copth.setTH025(1.0000);
            copth.setTH026("N");
            copth.setTH030("********************");
            copth.setTH031("1");
            copth.setTH035(TO020.doubleValue()*shipmentQuantity);
            xhje += TO020.doubleValue()*shipmentQuantity;
            double th036 = TO020.doubleValue() * shipmentQuantity * TO034.doubleValue();
            copth.setTH036(th036);
            xhse += th036;
            copth.setTH037(coptg.getTG012()*TO020.doubleValue()*shipmentQuantity);
            bbxhje+=coptg.getTG012()*TO020.doubleValue()*shipmentQuantity;
            copth.setTH038(coptg.getTG012()*TO020.doubleValue()*shipmentQuantity*TO034.doubleValue());
            bbxhse+=coptg.getTG012()*TO020.doubleValue()*shipmentQuantity*TO034.doubleValue();
            copth.setTH013(th036 +TO020.doubleValue()*shipmentQuantity);
            BigDecimal TO030 = (BigDecimal) map1.get("TO030");
            copth.setTH039(TO030.doubleValue());
            BigDecimal TO031 = (BigDecimal) map1.get("TO031");
            copth.setTH040(TO031.doubleValue());
            copth.setTH041((String) map1.get("TO032"));
            copth.setTH042(0.000000);
            BigDecimal TO012 = (BigDecimal) map1.get("TO012");
            copth.setTH043(TO012.doubleValue());
            js += TO012.doubleValue();
            BigDecimal TO013 = (BigDecimal) map1.get("TO013");
            copth.setTH043(TO013.doubleValue());
            copth.setTH045((String) map1.get("TO001"));
            copth.setTH046((String) map1.get("TO002"));
            copth.setTH047((String) map1.get("TO003"));
            copth.setTH048(TO034.doubleValue());
            //TODO 49批发价 50零售价 暂时都是0 出货通知单没有对应字段
            copth.setTH049(0.000000);
            copth.setTH050(0.000000);
            copth.setTH051((String) map1.get("TO037"));
            copth.setTH052((String) map1.get("TO016"));
            copth.setTH053((String) map1.get("TO015"));
            copth.setTH055((String) map1.get("TO038"));
            copth.setTH056((String) map1.get("TO039"));
            copth.setTH060(0.000000);
            copth.setTH061(0.000000);
            copth.setTH062(0.000000);
            copth.setTHD01(0.000000);
            copth.setTH064("N");
            copth.setTHH01("N");
            copth.setTHS01("N");
            copth.setUDF51(new BigDecimal("0.000000"));
            copth.setUDF52(new BigDecimal("0.000000"));
            copth.setUDF53(new BigDecimal("0.000000"));
            copth.setUDF54(new BigDecimal("0.000000"));
            copth.setUDF55(new BigDecimal("0.000000"));
            copth.setUDF56(new BigDecimal("0.000000"));
            copth.setUDF57(new BigDecimal("0.000000"));
            copth.setUDF58(new BigDecimal("0.000000"));
            copth.setUDF59(new BigDecimal("0.000000"));
            copth.setUDF60(new BigDecimal("0.000000"));
            copth.setUDF61(new BigDecimal("0.000000"));
            copth.setUDF62(new BigDecimal("0.000000"));
            copths.add(copth);
        }
        coptg.setTG013(xhje);
        coptg.setTG016(customerInfo.getInvoiceType());
        coptg.setTG017(customerInfo.getTax());
        coptg.setTG020(salesSlipDto.getRemark());
        coptg.setTG022(1.0);
        coptg.setTG023("N");
        coptg.setTG024("N");
        coptg.setTG025(xhse);
        coptg.setTG026(customerInfo.getEmpNo());
        //TODO 31 烟酒注记
        coptg.setTG031("N");
        coptg.setTG032(js);
        coptg.setTG033(sl);
        coptg.setTG036("N");
        coptg.setTG037("N");
        coptg.setTG041(0.0);
        coptg.setTG042(yyyyMMdd);
        coptg.setTG043(salesSlipDto.getCreator());
        coptg.setTG044(customerInfo.getRate());
        coptg.setTG045(bbxhje);
        coptg.setTG046(bbxhse);
        coptg.setTG047(customerInfo.getPaymentNo());
        coptg.setTG052(0.000000);
        coptg.setTG053(0.000000);
        coptg.setTG054(0.000000);
        coptg.setTG055("N");
        coptg.setTG059("N");
        coptg.setTG067(0.0);
        coptg.setTG068("N");
        coptg.setTG071(0.000000);
        coptg.setTG072(0.000000);
        coptg.setTG073(0.000000);
        coptg.setTG074("N");
        coptg.setTG075("********************");
        coptg.setTG076("N");
        coptg.setTG079("N");
        coptg.setUDF51(new BigDecimal("0.000000"));
        coptg.setUDF52(new BigDecimal("0.000000"));
        coptg.setUDF53(new BigDecimal("0.000000"));
        coptg.setUDF54(new BigDecimal("0.000000"));
        coptg.setUDF55(new BigDecimal("0.000000"));
        coptg.setUDF56(new BigDecimal("0.000000"));
        coptg.setUDF57(new BigDecimal("0.000000"));
        coptg.setUDF58(new BigDecimal("0.000000"));
        coptg.setUDF59(new BigDecimal("0.000000"));
        coptg.setUDF60(new BigDecimal("0.000000"));
        coptg.setUDF61(new BigDecimal("0.000000"));
        coptg.setUDF62(new BigDecimal("0.000000"));

        salesMapper.insertCoptg(coptg);
        for (Copth copth : copths) {
            salesMapper.insertCopth(copth);
        }

//        ShenHe shenHe = new ShenHe();
//        shenHe.setCom("ZJHZ");
//        shenHe.setProgram("COPI08");
//        shenHe.setUser(salesSlipDto.getCreator());
//        shenHe.setDb("ASHO");
//        shenHe.setDh("" + dh);
//        shenHe.setType("DO");
//        shenHe.setRq(yyyyMMdd);
//
//        InetAddress localhost = InetAddress.getLocalHost();
//        String ipAddress = localhost.getHostAddress();
//        String url = "http://" + "192.168.1.47" + ":9000/api/yifei/audit";
//
//        String jsonString = JSON.toJSONString(shenHe);
//        String ans = null;
//        try {
//            ans = HttpClientUtil.postRequest(url, jsonString);
//            HttpResult httpResult = JSON.parseObject(ans, HttpResult.class);
//            if (httpResult.getStatus().equals("失败")) {
//                salesMapper.deleteCoptg(coptg);
//                for (Copth copth : copths) {
//                    salesMapper.deleteCopth(copth);
//                }
//                map.put("ASHO"+dh,"生成入库单失败");
//                return map;
//            }
//        } catch (Exception e) {
//            System.out.println("出现异常请查看审批后台是否开启");
//            salesMapper.deleteCoptg(coptg);
//            for (Copth copth : copths) {
//                salesMapper.deleteCopth(copth);
//            }
//            map.put("ASHO"+dh,"生成入库单失败");
//            return map;
//        }
        map.put("ASHO"+dh,"生成出库单成功");
        return map;
    }

    @Override
    public Map<String, String> salesSlipByOrder(salesSlipDto salesSlipDto) throws UnknownHostException {
        int i = 1;
        //原币销货金额
        double xhje = 0.0;
        //原币销货税额
        double xhse = 0.0;
        //本币销货金额
        double bbxhje = 0.0;
        //本币销货税额
        double bbxhse = 0.0;
        //件数
        double js = 0.0;
        //数量
        double sl = 0.0;
        Map<String,String> map = new HashMap<>();
        Coptg coptg = new Coptg();
        coptg.setTG001("ASHO");
        CustomerInfoVo customerInfo = salesSlipDto.getCustomerInfoVo();
        coptg.setCREATOR(salesSlipDto.getCreator());
        String usrGroup = commonMapper.getUsrGroup(salesSlipDto.getCreator());
        String company = commonMapper.getCompany();
        coptg.setUSR_GROUP(usrGroup);
        coptg.setCOMPANY(company);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

        // 格式化时间
        String yyyyMM = currentTime.format(formatter);
        String yyyyMMdd = currentTime.format(formatter1);
        String create_date = currentTime.format(formatter2);

        coptg.setCREATE_DATE(create_date);
        BigDecimal flag = new BigDecimal("1");
        coptg.setFLAG(flag);
        //TODO 可以根据单据性质产生单号
        Long dh = commonMapper.getDH("COPTG","TG002");
        Long time = Long.parseLong(yyyyMMdd + "001");
        if (dh == null ||dh < time) {
            dh = time;
        } else {
            dh++;
        }
        coptg.setTG002(""+dh);
        coptg.setTG003(yyyyMMdd);
        coptg.setTG004(customerInfo.getCustomerNo());
        coptg.setTG005(customerInfo.getDeptNo());
        coptg.setTG006(customerInfo.getEmpNo());
        coptg.setTG008(customerInfo.getAddressNo1());
        coptg.setTG009(customerInfo.getAddressNo2());
        //TODO 工厂只有一个暂时写死
        coptg.setTG010("01");
        coptg.setTG011(customerInfo.getCurrency());
        if (customerInfo.getCurrency().equals("RMB")){
            coptg.setTG012(1.0000000);
        }else {
            coptg.setTG012(salesMapper.selectHL(customerInfo.getCurrency()));
        }
        ArrayList<Copth> copths = new ArrayList<>();
        for (salesSlipBodyDto salesSlipBodyDto : salesSlipDto.getSalesSlipBodyDtos()) {
            Copth copth = new Copth();
            String xh = String.format("%04d", i++);
            copth.setCOMPANY(company);
            copth.setCREATOR(salesSlipDto.getCreator());
            copth.setUSR_GROUP(usrGroup);
            copth.setCREATE_DATE(create_date);
            copth.setFLAG(flag);
            copth.setTH001("ASHO");
            copth.setTH002(""+dh);
            copth.setTH003(xh);
            String order = salesSlipBodyDto.getCustomerOrder().getDb()+salesSlipBodyDto.getCustomerOrder().getDh()+salesSlipBodyDto.getCustomerOrder().getXh();
            Map<String,Object> map1 = salesMapper.getBodyInfoByOrder(order);
            if (map1==null){
                map.put("此单号未查询到数据:",order);
                continue;
            }
            copth.setTH004((String) map1.get("TD004"));
            copth.setTH005((String) map1.get("TD005"));
            copth.setTH006((String) map1.get("TD006"));
            copth.setTH007((String) map1.get("TD007"));
            Double saleOrderNum = salesSlipBodyDto.getCustomerOrder().getSaleOrderNum();
            copth.setTH008(saleOrderNum);
            sl += saleOrderNum;
            copth.setTH009((String) map1.get("TD010"));
            //TODO 库存数量不知道哪里取默认都为0
            copth.setTH010(0.000000);
            BigDecimal TD011 = (BigDecimal) map1.get("TD011");
            copth.setTH012(TD011.doubleValue());
            BigDecimal TD012 = (BigDecimal) map1.get("TD012");
            copth.setTH013(TD012.doubleValue());
            copth.setTH014((String) map1.get("TD001"));
            copth.setTH015((String) map1.get("TD002"));
            copth.setTH016((String) map1.get("TD003"));
            //TODO 批号还需要修改
            copth.setTH017(salesSlipBodyDto.getLotVo().getLot());
            copth.setTH018(salesSlipBodyDto.getRemark());
            copth.setTH019((String) map1.get("TD014"));
            copth.setTH020("N");
            copth.setTH021("N");
            BigDecimal TD024 = (BigDecimal) map1.get("TD024");
            BigDecimal TD025 = (BigDecimal) map1.get("TD025");
            copth.setTH024(TD024.doubleValue()- TD025.doubleValue());
            //TODO 折扣率默认1.0000
            copth.setTH025(1.0000);
            copth.setTH026("N");
            copth.setTH030("********************");
            copth.setTH031("1");
            double v = TD011.doubleValue() * saleOrderNum;
            copth.setTH035(v);
            xhje += v;
            copth.setTH036(v*customerInfo.getRate());
            xhse += v*customerInfo.getRate();
            copth.setTH037(v * coptg.getTG012());
            bbxhje+=v * coptg.getTG012();
            copth.setTH038(v * coptg.getTG012()*customerInfo.getRate());
            bbxhse+= v * coptg.getTG012()*customerInfo.getRate();
            BigDecimal TD032 = (BigDecimal) map1.get("TD032");
            BigDecimal TD033 = (BigDecimal) map1.get("TD033");
            copth.setTH039(TD032.doubleValue()-TD033.doubleValue());
            BigDecimal TD034 = (BigDecimal) map1.get("TD034");
            BigDecimal TD035 = (BigDecimal) map1.get("TD035");
            copth.setTH040(TD034.doubleValue()- TD035.doubleValue());
            copth.setTH041((String) map1.get("TD036"));
            copth.setTH042(0.000000);
            BigDecimal TD040 = (BigDecimal) map1.get("TD040");
            copth.setTH043(TD040.doubleValue());
            js += TD040.doubleValue();
            BigDecimal TD041 = (BigDecimal) map1.get("TD041");
            copth.setTH044(TD041.doubleValue());
            BigDecimal TD037 = (BigDecimal) map1.get("TD037");
            copth.setTH048(TD037.doubleValue());
            BigDecimal TD042 = (BigDecimal) map1.get("TD042");
            copth.setTH049(TD042.doubleValue());
            BigDecimal TD043 = (BigDecimal) map1.get("TD043");
            copth.setTH050(TD043.doubleValue());
            if (!salesSlipBodyDto.getLotVo().getLot().equals("********************")) {
                copth.setTH051(salesSlipBodyDto.getLotVo().getProDate());
                copth.setTH052(salesSlipBodyDto.getLotVo().getEffectiveDate());
                copth.setTH053(salesSlipBodyDto.getLotVo().getRecheckDate());
            }
            copth.setTH056((String) map1.get("TDD05"));
            copth.setTH060(0.000000);
            copth.setTH061(0.000000);
            copth.setTH062(0.000000);
            copth.setTHD01(0.000000);
            copth.setTH064("N");
            copth.setTHH01("N");
            copth.setTHS01("N");
            copth.setUDF51(new BigDecimal("0.000000"));
            copth.setUDF52(new BigDecimal("0.000000"));
            copth.setUDF53(new BigDecimal("0.000000"));
            copth.setUDF54(new BigDecimal("0.000000"));
            copth.setUDF55(new BigDecimal("0.000000"));
            copth.setUDF56(new BigDecimal("0.000000"));
            copth.setUDF57(new BigDecimal("0.000000"));
            copth.setUDF58(new BigDecimal("0.000000"));
            copth.setUDF59(new BigDecimal("0.000000"));
            copth.setUDF60(new BigDecimal("0.000000"));
            copth.setUDF61(new BigDecimal("0.000000"));
            copth.setUDF62(new BigDecimal("0.000000"));
            copths.add(copth);
        }
        coptg.setTG013(xhje);
        coptg.setTG016(customerInfo.getInvoiceType());
        coptg.setTG017(customerInfo.getTax());
        coptg.setTG020(salesSlipDto.getRemark());
        coptg.setTG022(1.0);
        coptg.setTG023("N");
        coptg.setTG024("N");
        coptg.setTG025(xhse);
        coptg.setTG026(customerInfo.getEmpNo());
        //TODO 31 烟酒注记
        coptg.setTG031("N");
        coptg.setTG032(js);
        coptg.setTG033(sl);
        coptg.setTG036("N");
        coptg.setTG037("N");
        coptg.setTG041(0.0);
        coptg.setTG042(yyyyMMdd);
        coptg.setTG043(salesSlipDto.getCreator());
        coptg.setTG044(customerInfo.getRate());
        coptg.setTG045(bbxhje);
        coptg.setTG046(bbxhse);
        coptg.setTG047(customerInfo.getPaymentNo());
        coptg.setTG052(0.000000);
        coptg.setTG053(0.000000);
        coptg.setTG054(0.000000);
        coptg.setTG055("N");
        coptg.setTG059("N");
        coptg.setTG067(0.0);
        coptg.setTG068("N");
        coptg.setTG071(0.000000);
        coptg.setTG072(0.000000);
        coptg.setTG073(0.000000);
        coptg.setTG074("N");
        coptg.setTG075("********************");
        coptg.setTG076("N");
        coptg.setTG079("N");
        coptg.setUDF51(new BigDecimal("0.000000"));
        coptg.setUDF52(new BigDecimal("0.000000"));
        coptg.setUDF53(new BigDecimal("0.000000"));
        coptg.setUDF54(new BigDecimal("0.000000"));
        coptg.setUDF55(new BigDecimal("0.000000"));
        coptg.setUDF56(new BigDecimal("0.000000"));
        coptg.setUDF57(new BigDecimal("0.000000"));
        coptg.setUDF58(new BigDecimal("0.000000"));
        coptg.setUDF59(new BigDecimal("0.000000"));
        coptg.setUDF60(new BigDecimal("0.000000"));
        coptg.setUDF61(new BigDecimal("0.000000"));
        coptg.setUDF62(new BigDecimal("0.000000"));

        salesMapper.insertCoptg(coptg);
        for (Copth copth : copths) {
            salesMapper.insertCopth(copth);
        }

//        ShenHe shenHe = new ShenHe();
//        shenHe.setCom("ZJHZ");
//        shenHe.setProgram("COPI08");
//        shenHe.setUser(salesSlipDto.getCreator());
//        shenHe.setDb("ASHO");
//        shenHe.setDh("" + dh);
//        shenHe.setType("DO");
//        shenHe.setRq(yyyyMMdd);
//
//        InetAddress localhost = InetAddress.getLocalHost();
//        String ipAddress = localhost.getHostAddress();
//        String url = "http://" + "192.168.1.47" + ":9000/api/yifei/audit";
//
//        String jsonString = JSON.toJSONString(shenHe);
//        String ans = null;
//        try {
//            ans = HttpClientUtil.postRequest(url, jsonString);
//            HttpResult httpResult = JSON.parseObject(ans, HttpResult.class);
//            if (httpResult.getStatus().equals("失败")) {
//                salesMapper.deleteCoptg(coptg);
//                for (Copth copth : copths) {
//                    salesMapper.deleteCopth(copth);
//                }
//                map.put("ASHO"+dh,"生成入库单失败");
//                return map;
//            }
//        } catch (Exception e) {
//            System.out.println("出现异常请查看审批后台是否开启");
//            salesMapper.deleteCoptg(coptg);
//            for (Copth copth : copths) {
//                salesMapper.deleteCopth(copth);
//            }
//            map.put("ASHO"+dh,"生成入库单失败");
//            return map;
//        }
        map.put("ASHO"+dh,"生成出库单成功");
        return map;
    }
}
